package com.kefu.boss.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.kefu.boss.model.BossUser;
import com.kefu.boss.model.BossUserRole;
import com.kefu.common.vo.AbstractUpdateVo;
import com.kefu.sys.model.SysUserRole;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@ApiModel(description = "")
public class BossUserUpdateVo extends AbstractUpdateVo<BossUser, String> {
    @NotEmpty
    @ApiModelProperty(value = "主键")
    private String userId;
    @ApiModelProperty(value = "名称")
    private String userName;
    @ApiModelProperty(value = "密码")
    private String userPwd;
    @ApiModelProperty(value = "状态：0停用，1启用")
    private Integer userStatus;
    @ApiModelProperty(name = "roleIds", value = "角色id")
    private String[] roleIds;
    @ApiModelProperty(value = "邮箱")
    private String userEmail;

    @Override
    public BossUser convert() {
        BossUser info = BeanUtil.toBean(this, BossUser.class);
        if (StrUtil.isNotBlank(userPwd)) {
            info.setUserPwd(new BCryptPasswordEncoder().encode(userPwd));
        }
        return info;
    }

    public List<BossUserRole> roleConvert(String userId) {
        List<BossUserRole> list = new ArrayList<>();
        for (String roleId : roleIds) {
            BossUserRole bossUserRole = new BossUserRole();
            bossUserRole.setRoleId(roleId);
            bossUserRole.setUserId(userId);
            list.add(bossUserRole);
        }
        return list;
    }
}